home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / u_man / cat3 / Tcl / regexp.z / regexp
Encoding:
Text File  |  2002-10-03  |  8.6 KB  |  199 lines

  1.  
  2.  
  3.  
  4. rrrreeeeggggeeeexxxxpppp((((3333TTTTccccllll))))                                                      rrrreeeeggggeeeexxxxpppp((((3333TTTTccccllll))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      regexp - Match a regular expression against a string
  10.  
  11. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.      rrrreeeeggggeeeexxxxpppp ?_s_w_i_t_c_h_e_s? _e_x_p _s_t_r_i_n_g ?_m_a_t_c_h_V_a_r? ?_s_u_b_M_a_t_c_h_V_a_r _s_u_b_M_a_t_c_h_V_a_r ...?
  13.  
  14.  
  15. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  16.      Determines whether the regular expression _e_x_p matches part or all of
  17.      _s_t_r_i_n_g and returns 1 if it does, 0 if it doesn't.
  18.  
  19.      If additional arguments are specified after _s_t_r_i_n_g then they are treated
  20.      as the names of variables in which to return information about which
  21.      part(s) of _s_t_r_i_n_g matched _e_x_p.  _M_a_t_c_h_V_a_r will be set to the range of
  22.      _s_t_r_i_n_g that matched all of _e_x_p.  The first _s_u_b_M_a_t_c_h_V_a_r will contain the
  23.      characters in _s_t_r_i_n_g that matched the leftmost parenthesized
  24.      subexpression within _e_x_p, the next _s_u_b_M_a_t_c_h_V_a_r will contain the
  25.      characters that matched the next parenthesized subexpression to the right
  26.      in _e_x_p, and so on.
  27.  
  28.      If the initial arguments to rrrreeeeggggeeeexxxxpppp start with ---- then they are treated as |
  29.      switches.  The following switches are currently supported:
  30.  
  31.      ----nnnnooooccccaaaasssseeee   Causes upper-case characters in _s_t_r_i_n_g to be treated as lower  |
  32.                case during the matching process.
  33.  
  34.      ----iiiinnnnddddiiiicccceeeessss  Changes what is stored in the _s_u_b_M_a_t_c_h_V_a_rs. Instead of storing |
  35.                the matching characters from ssssttttrrrriiiinnnngggg, each variable will contain|
  36.                a list of two decimal strings giving the indices in _s_t_r_i_n_g of  |
  37.                the first and last characters in the matching range of         |
  38.                characters.
  39.  
  40.      --------        Marks the end of switches.  The argument following this one    |
  41.                will be treated as _e_x_p even if it starts with a ----.
  42.  
  43.      If there are more _s_u_b_M_a_t_c_h_V_a_r's than parenthesized subexpressions within
  44.      _e_x_p, or if a particular subexpression in _e_x_p doesn't match the string
  45.      (e.g. because it was in a portion of the expression that wasn't matched),
  46.      then the corresponding _s_u_b_M_a_t_c_h_V_a_r will be set to ``----1111 ----1111'' if ----iiiinnnnddddiiiicccceeeessss
  47.      has been specified or to an empty string otherwise.
  48.  
  49.  
  50. RRRREEEEGGGGUUUULLLLAAAARRRR EEEEXXXXPPPPRRRREEEESSSSSSSSIIIIOOOONNNNSSSS
  51.      Regular expressions are implemented using Henry Spencer's package
  52.      (thanks, Henry!), and much of the description of regular expressions
  53.      below is copied verbatim from his manual entry.
  54.  
  55.      A regular expression is zero or more _b_r_a_n_c_h_e_s, separated by ``|''.  It
  56.      matches anything that matches one of the branches.
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. rrrreeeeggggeeeexxxxpppp((((3333TTTTccccllll))))                                                      rrrreeeeggggeeeexxxxpppp((((3333TTTTccccllll))))
  71.  
  72.  
  73.  
  74.      A branch is zero or more _p_i_e_c_e_s, concatenated.  It matches a match for
  75.      the first, followed by a match for the second, etc.
  76.  
  77.      A piece is an _a_t_o_m possibly followed by ``*'', ``+'', or ``?''.  An atom
  78.      followed by ``*'' matches a sequence of 0 or more matches of the atom.
  79.      An atom followed by ``+'' matches a sequence of 1 or more matches of the
  80.      atom.  An atom followed by ``?'' matches a match of the atom, or the null
  81.      string.
  82.  
  83.      An atom is a regular expression in parentheses (matching a match for the
  84.      regular expression), a _r_a_n_g_e (see below), ``.''  (matching any single
  85.      character), ``^'' (matching the null string at the beginning of the input
  86.      string), ``$'' (matching the null string at the end of the input string),
  87.      a ``\'' followed by a single character (matching that character), or a
  88.      single character with no other significance (matching that character).
  89.  
  90.      A _r_a_n_g_e is a sequence of characters enclosed in ``[]''.  It normally
  91.      matches any single character from the sequence.  If the sequence begins
  92.      with ``^'', it matches any single character _n_o_t from the rest of the
  93.      sequence.  If two characters in the sequence are separated by ``-'', this
  94.      is shorthand for the full list of ASCII characters between them (e.g.
  95.      ``[0-9]'' matches any decimal digit).  To include a literal ``]'' in the
  96.      sequence, make it the first character (following a possible ``^'').  To
  97.      include a literal ``-'', make it the first or last character.
  98.  
  99.  
  100. CCCCHHHHOOOOOOOOSSSSIIIINNNNGGGG AAAAMMMMOOOONNNNGGGG AAAALLLLTTTTEEEERRRRNNNNAAAATTTTIIIIVVVVEEEE MMMMAAAATTTTCCCCHHHHEEEESSSS
  101.      In general there may be more than one way to match a regular expression
  102.      to an input string.  For example, consider the command
  103.  
  104.           rrrreeeeggggeeeexxxxpppp  ((((aaaa****))))bbbb****  aaaaaaaabbbbaaaaaaaaaaaabbbbbbbb  xxxx  yyyy
  105.  
  106.      Considering only the rules given so far, xxxx and yyyy could end up with the
  107.      values aaaaaaaabbbbbbbb and aaaaaaaa, aaaaaaaaaaaabbbb and aaaaaaaaaaaa, aaaabbbb and aaaa, or any of several other
  108.      combinations.  To resolve this potential ambiguity rrrreeeeggggeeeexxxxpppp chooses among
  109.      alternatives using the rule ``first then longest''.  In other words, it
  110.      considers the possible matches in order working from left to right across
  111.      the input string and the pattern, and it attempts to match longer pieces
  112.      of the input string before shorter ones.  More specifically, the
  113.      following rules apply in decreasing order of priority:
  114.  
  115.      [1]  If a regular expression could match two different parts of an input
  116.           string then it will match the one that begins earliest.
  117.  
  118.      [2]  If a regular expression contains |||| operators then the leftmost
  119.           matching sub-expression is chosen.
  120.  
  121.      [3]  In ****, ++++, and ???? constructs, longer matches are chosen in preference
  122.           to shorter ones.
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. rrrreeeeggggeeeexxxxpppp((((3333TTTTccccllll))))                                                      rrrreeeeggggeeeexxxxpppp((((3333TTTTccccllll))))
  137.  
  138.  
  139.  
  140.      [4]  In sequences of expression components the components are considered
  141.           from left to right.
  142.  
  143.      In the example from above, ((((aaaa****))))bbbb**** matches aaaaaaaabbbb:  the ((((aaaa****)))) portion of the
  144.      pattern is matched first and it consumes the leading aaaaaaaa; then the bbbb****
  145.      portion of the pattern consumes the next bbbb.  Or, consider the following
  146.      example:
  147.  
  148.           rrrreeeeggggeeeexxxxpppp  ((((aaaabbbb||||aaaa))))((((bbbb****))))cccc  aaaabbbbcccc  xxxx  yyyy  zzzz
  149.  
  150.      After this command xxxx will be aaaabbbbcccc, yyyy will be aaaabbbb, and zzzz will be an empty
  151.      string.  Rule 4 specifies that ((((aaaabbbb||||aaaa)))) gets first shot at the input string
  152.      and Rule 2 specifies that the aaaabbbb sub-expression is checked before the aaaa
  153.      sub-expression.  Thus the bbbb has already been claimed before the ((((bbbb****))))
  154.      component is checked and ((((bbbb****)))) must match an empty string.
  155.  
  156.  
  157. KKKKEEEEYYYYWWWWOOOORRRRDDDDSSSS
  158.      match, regular expression, string
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.